package cn.yitun.chat.yitunservermanagecore.general.manage.system.controller;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitungeneral.annotation.parameter.ParamDefine;
import cn.yitun.chat.yitungeneral.annotation.parameter.RequestParameter;
import cn.yitun.chat.yitunservercommonlib.annotation.Log;
import cn.yitun.chat.yitunservercommonlib.basebean.web.JwtUserDto;
import cn.yitun.chat.yitunservercommonlib.converter.dto2po.system.RoleConverter;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.BaseController;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.RoleDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.RoleVo;
import cn.yitun.chat.yitunservermanagecore.general.bridge.auth.annotation.PermissionMapping;
import cn.yitun.chat.yitunservermanagecore.general.manage.system.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * date 2018-06-04 14:59:44<br>
 * description
 * 
 * @author yitun<br>
 * @since
 */
@RestController
@RequestMapping("/manage/system")
public class RoleController extends BaseController{

	@Autowired
	RoleService roleService;

	@RequestParameter
	@PermissionMapping(name = "角色列表", perms = "sys:role:list")
	@RequestMapping(method = RequestMethod.POST, value = "/role/page")
	public Object list(@JsonDefine RoleDto roleQuery) {
		ResultMessage rm = new ResultMessage();
		PageHelper.startPage(roleQuery.getCurrentPageNo(), roleQuery.getPageSize());
		List<RoleVo> list = roleService.queryList(roleQuery);
		rm.bodyPut("page", new PageInfo<>(list));
		return rm;
	}

	@Log("角色新增")
	@RequestParameter
	@PermissionMapping(name = "角色新增", perms = "sys:role:add")
	@PostMapping("/role/save")
	Object save(JwtUserDto jwtUserDto, @JsonDefine RoleDto roleDto) {
		setUserMsgSave(roleDto, jwtUserDto);
		if (roleService.save(roleDto) == 0)
			throw new YitunException(ExceptionEnum.ADD_FAIL);
		return  new ResultMessage();
	}

	@Log("角色修改")
	@RequestParameter
	@PermissionMapping(name = "角色修改", perms = "sys:role:update")
	@PutMapping("/role/update")
	Object update(JwtUserDto jwtUserDto, @JsonDefine RoleDto roleDto) {
		setUserMsgUpdate(roleDto, jwtUserDto);
		if (roleDto.getId() == null)
			throw new YitunException(ExceptionEnum.REQUEST_PARAM_NULL);
		if (roleService.update(roleDto) == 0)
			throw new YitunException(ExceptionEnum.ADD_FAIL);
		return new ResultMessage();
	}

	@RequestParameter
	@PermissionMapping(name = "获取角色", perms = "sys:role:info")
	@RequestMapping(method = RequestMethod.GET, value = "/role/get")
	public Object get(@ParamDefine Long id) {
		ResultMessage rm = new ResultMessage();
		try {
			RoleVo role = roleService.getVo(id);
			rm.bodyPut("role", role);
		} catch (Exception e) {
			e.printStackTrace();
			rm.addError("500", "系统异常");
		}
		return rm;
	}

	@Log("角色删除")
	@RequestParameter
	@PermissionMapping(name = "删除角色", perms = "sys:role:remove")
	@DeleteMapping("/role/remove/{id}")
	Object remove(@PathVariable Long id) {
		if (roleService.remove(id) == 0)
			throw new YitunException(ExceptionEnum.DELETE_FAILED);
		return  new ResultMessage();
	}

	@Log("角色批量删除")
	@RequestParameter
	@PermissionMapping(name = "角色批量删除", perms = "sys:role:remove")
	@DeleteMapping("/role/batchRemove/{ids}")
	Object batchRemove(@PathVariable Long[] ids) {
		if (roleService.batchRemove(ids) == 0)
			throw new YitunException(ExceptionEnum.DELETE_FAILED);
		return  new ResultMessage();
	}

	@RequestParameter
	@PermissionMapping(name = "所有角色列表", perms = "sys:role:list")
	@RequestMapping(method = RequestMethod.POST, value = "/role/allList")
	public Object allList() {
		ResultMessage rm = new ResultMessage();
		try {
			List<RoleVo> list = roleService.listVo(null);
			rm.bodyPut("list", list);
		} catch (Exception e) {
			e.printStackTrace();
			rm.addError("500", "系统异常");
		}
		return rm;
	}

	@RequestParameter
	@PermissionMapping(name = "获取用户角色id", perms = "sys:user:info")
	@GetMapping("/role/userId/{userId}")
	public Object roleIdByUserId( @PathVariable Long userId){
		ResultMessage rm = new ResultMessage();
		List<Long> roleIds = roleService.roleIdsByUserId(userId);
		rm.bodyPut("roleList", roleIds);
		return rm ;
	}
	@RequestParameter
	@PermissionMapping(name = "用户角色列表", perms = "sys:user:info")
	@PostMapping("/role/roleList")
	Object roleList(JwtUserDto jwtUserDto, @JsonDefine RoleDto roleDto) {
		//查询列表数据
		roleDto.setCurrentUserId(jwtUserDto.getUserId());
		List<RoleVo> roles = roleService.listVO(roleDto);
		ResultMessage rm = new ResultMessage();
		rm.bodyPut("list", roles);
		return rm;
	}

	/**
	 * 校验角色名称
	 */
	@RequestParameter
	@PermissionMapping(name = "校验角色名称", perms = "sys:role:role")
	@PostMapping("/role/checkRoleNameUnique")
	public Object checkRoleNameUnique(@JsonDefine RoleDto roleDto) {
		ResultMessage rm = new ResultMessage();
		rm.bodyPut("isUnique", roleService.checkRoleNameUnique(RoleConverter.INSTANCE.dto2do(roleDto) ));
		return rm;
	}

	/**
	 * 校验角色权限
	 */
	@RequestParameter
	@PermissionMapping(name = "校验角色权限", perms = "sys:role:role")
	@PostMapping("/role/checkRoleSignUnique")
	public Object checkRoleSignUnique(@JsonDefine RoleDto roleDto) {
		ResultMessage rm = new ResultMessage();
		rm.bodyPut("isUnique", roleService.checkRoleSignUnique(RoleConverter.INSTANCE.dto2do(roleDto) ));
		return rm;
	}

	/**
	 * 查询用户角色
	 */
	@RequestParameter
	@PermissionMapping(name = "查询用户角色", perms = "sys:user:info")
	@GetMapping("/role/selectRoleKeys")
	public Object selectRoleKeys(@ParamDefine("userId") Long userId) {
		ResultMessage rm = new ResultMessage();
		rm.bodyPut("list", roleService.selectRoleKeys(userId));
		return rm;
	}

}
